// pages/content/login.js
var api = require('../../utils/api.js');
var mdk = require('../../utils/md55.js');
var rsa = require('../../utils/rsa.js');
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    array: [],
    index: 0,
    telephonenum: '',
    password: '',

    merchanNum: '',
    shopNum: '',
    flag: true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.getStorage({
      key: 'merNo',
      success: function (res) {
        that.setData({
          merchanNum: res.data
        })
      },
    })
    wx.getStorage({
      key: 'shopNo',
      success: function (res) {
        that.setData({
          shopNum: res.data
        })
      },
    })
    wx.getStorage({
      key: 'phoneNum',
      success: function (res) {
        that.setData({
          telephonenum: res.data
        })
      },
    })
    wx.getStorage({
      key: 'pwdNum',
      success: function (res) {
        that.setData({
          password: res.data
        })
      },
    })
  },
  //登陆
  loginAction: function () {
    const tel = this.data.telephonenum;
    var pwd = this.data.password;

    var password = mdk.hexMD5(pwd);
    var merNum = this.data.merchanNum;
    var shop = this.data.shopNum;
    const flg = this.data.flag;
    app.globalData.tel = tel;
    if (flg) {
      try {
        wx.setStorageSync('phoneNum', tel)
        wx.setStorageSync('pwdNum', pwd)
      } catch (e) { }
    } else {
      try {
        wx.setStorageSync('phoneNum', '')
        wx.setStorageSync('pwdNum', '')
      } catch (e) { }
    }
    app.globalData.merNo = merNum;
    wx.showLoading({
      title: '加载中...',
    })
    rsa.Cpic_fetchPost(rsa.VSERVERPORT, {
      "method": 'promotion.signin',
      "user_name": tel,
      "pass_word": password,
      'seller_bn': ''
    }, (err, res) => {
      wx.hideLoading();
      if (err) {
        wx.showModal({
          title: '登录失败',
          content: err,
          showCancel: false,
          success: function (res) {
            if (res.confirm) {

            } else if (res.cancel) {

            }
          }
        })
        return;
      }

      const aditstatus = res.auditStatus;
      const merid = res.merId;
      const code = res.return_code;
      const session = res.sessionId;
      if (code == '0000') {
        app.globalData.merId = merid;
        if (aditstatus == '0') { //未认证
          wx.showModal({
            title: '登录成功',
            content: '账户未认证，是否去认证',
            confirmText: '是',
            cancelText: '否',
            success: function (res) {
              if (res.confirm) {
                wx.navigateTo({
                  url: '/pages/userCenter/merIdentify',
                })
              } else if (res.cancel) {
                app.globalData.userstatus = 0;
                wx.navigateTo({
                  url: '/pages/content/tindex?telphone=' + tel,
                })
              }
            }
          })

        } else if (aditstatus == '2') {
          const session = res.sessionId; //会话ID
          app.globalData.session = session;
          app.globalData.merNo = res.sellerId;
          app.globalData.shoopnum = res.storeBn;
          app.globalData.userstatus = 2;
          app.globalData.shopName = res.storeName;
          wx.navigateTo({
            url: '/pages/content/tindex?telphone=' + tel,
          })
        } else {
          app.globalData.userstatus = 1;
          wx.showModal({
            title: '提示',
            content: '审核中，请耐心等待审核',
            showCancel: false,
            success:function(res){
              wx.navigateTo({
                url: '/pages/content/tindex?telphone=' + tel,
              })
            }
          })
        }
      } else {
        wx.showModal({
          title: '登录失败',
          content: res.return_msg,
          showCancel: false,
        })
      }
    })
  },
  //记住用户名密码
  switch1Change: function (e) {
    const flag1 = e.detail.value;
    this.setData({
      flag: flag1,
    })
  },
  bindPickerChange: function (e) {
    const value = e.detail.value;
    this.setData({
      index: value,
    })
  },
  //输入密码
  bindpasswordInput: function (e) {
    const vale = e.detail.value;
    this.setData({
      password: vale,
    })
  },
  //输入商户号
  bindmerchantNum: function (e) {
    const vale = e.detail.value;
    this.setData({
      merchanNum: vale,
    })
  },
  //输入手机号
  bindNameInput: function (e) {
    const inputValue = e.detail.value;
    this.setData({
      telephonenum: inputValue,
    })
  },

  //忘记密码
  forgetPwd: function () {
    wx.navigateTo({
      url: '../content/fogetpwd',
    })
  },
  //门店号
  bindshopNum: function (e) {
    this.data.shopNum = e.detail.value;
  },
  //用户注册
  userRegist: function () {
    wx.navigateTo({
      url: '/pages/userCenter/registuser',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})